package com.stylefeng.guns.modular.vpn_package_info.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.vpn.dto.PackageDTO;
import com.stylefeng.guns.modular.vpn_package_info.dto.AddDiscountDTO;
import com.stylefeng.guns.modular.vpn_package_info.dto.PackageInfoDTO;
import com.stylefeng.guns.modular.vpn_package_info.entity.VpnPackageItemDO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.VpnPackageInfo;
import com.stylefeng.guns.modular.vpn_package_info.service.IVpnPackageInfoService;

import java.util.List;

/**
 * 套餐管理控制器
 *
 * @author fengshuonan
 * @Date 2020-03-02 16:20:38
 */
@Controller
@RequestMapping("/vpnPackageInfo")
public class VpnPackageInfoController extends BaseController {

    private String PREFIX = "/vpn_package_info/vpnPackageInfo/";

    @Autowired
    private IVpnPackageInfoService vpnPackageInfoService;

    /**
     * 跳转到套餐管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vpnPackageInfo.html";
    }

    /**
     * 跳转到添加套餐管理
     */
    @RequestMapping("/vpnPackageInfo_add")
    public String vpnPackageInfoAdd() {
        return PREFIX + "vpnPackageInfo_add.html";
    }

    /**
     * 跳转到添加折扣信息页面
     */
    @RequestMapping("/vpnPackageInfo_discount_add")
    public String vpnPackageDiscountInfoAdd() {
        return PREFIX + "vpnPackageInfo_discount_add.html";
    }

    /**
     * 跳转到修改套餐管理
     */
    @RequestMapping("/vpnPackageInfo_update/{vpnPackageInfoId}")
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public String vpnPackageInfoUpdate(@PathVariable Integer vpnPackageInfoId, Model model) {
        List<VpnPackageItemDO> list = vpnPackageInfoService.findPackageItemByPackageId(vpnPackageInfoId);

        VpnPackageItemDO itemDO = new VpnPackageItemDO();
        if (list != null && list.size() > 0){
            itemDO = list.get(0);
        }
        model.addAttribute("item",itemDO);
        LogObjectHolder.me().set(itemDO);
        return PREFIX + "vpnPackageInfo_edit.html";
    }

    /**
     * 获取套餐管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object list(String condition) {
        return vpnPackageInfoService.packageList();
    }

    /**
     * 新增套餐管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PackageDTO packageDTO) {
        vpnPackageInfoService.addPackage(packageDTO);
        return SUCCESS_TIP;
    }

    /**
     * 删除套餐管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object delete(@RequestParam Integer vpnPackageInfoId) {
        vpnPackageInfoService.deleteById(vpnPackageInfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改套餐管理
     */
    @RequestMapping(value = "/updateStatus")
    @ResponseBody
    public Object update(@RequestParam("packageId") Integer packageId,
                         @RequestParam("status") Integer status) {
        vpnPackageInfoService.updatePackageStatus(packageId,status);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/updatePackage")
    @ResponseBody
    public Object updatePackage(PackageInfoDTO dto) {
        vpnPackageInfoService.updatePackage(dto);
        return SUCCESS_TIP;
    }

    /**
     * 套餐管理详情
     */
    @RequestMapping(value = "/detail/{vpnPackageInfoId}")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object detail(@PathVariable("vpnPackageInfoId") Integer vpnPackageInfoId) {
        return vpnPackageInfoService.selectById(vpnPackageInfoId);
    }

    @RequestMapping(value = "/addDiscount")
    @ResponseBody
    public Object addDiscount(AddDiscountDTO dto) {
        vpnPackageInfoService.addDiscount(dto);
        return SUCCESS_TIP;
    }

    /**
     * 短数据套餐列表
     * @return
     */
    @RequestMapping(value = "/shortPackageList")
    @ResponseBody
    public Object shortPackageList() {
        return vpnPackageInfoService.shortVpnPackageList();
    }

    /**
     * 添加套餐的选项条件列表
     * @return
     */
    @RequestMapping(value = "/filterList")
    @ResponseBody
    public Object filterList() {
        return vpnPackageInfoService.filterList();
    }
}
